package com.wthink.newMain.settings.data

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentDataBinding
import com.wthink.newMain.settings.data.inner.DataCanFragment
import com.wthink.newMain.settings.data.inner.DataPortFragment
import com.wthink.newMain.settings.data.viewmodel.DataViewmodel

class DataFragment : Fragment(){
    private val viewModel by viewModels<DataViewmodel>()

    private lateinit var binding: FragmentDataBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_data,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity!!.supportFragmentManager
            .beginTransaction()
            .add(R.id.data_content, DataPortFragment())
            .commit()

        binding.port.isSelected = true

        binding.port.setOnClickListener {
            addFragment(DataPortFragment())
            binding.can.isSelected = false
            binding.port.isSelected = true
        }

        binding.can.setOnClickListener {
            addFragment(DataCanFragment())
            binding.can.isSelected = true
            binding.port.isSelected = false
        }
    }

    private fun addFragment(fragment: Fragment) {
        activity!!.supportFragmentManager
            .beginTransaction()
            //.setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
            .replace(R.id.data_content, fragment, fragment.javaClass.simpleName)
            .commit()
    }
}